package Interfaces;

import java.awt.BorderLayout;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;
import javax.swing.table.DefaultTableModel;

import calculator.auxiliary_I_Team;

import Boundary.UpdateStateObserverInv;

public class ShowStateObserversAndInvariants 
{
	private ShowStateObserversAndInvariants self = this;
	UpdateStateObserverInv updateStateObserverInv;
	JPanel container;
	JTable tab;
	
	public ShowStateObserversAndInvariants(UpdateStateObserverInv uSOI)
	{
		updateStateObserverInv=uSOI;
	}
	
	public JPanel init()
	{
		container = new JPanel();
		container.setLayout(new BorderLayout());
		
		JLabel label = new JLabel("Show State Observers and Invariant part");
		label.setFont(new Font("Tahoma", Font.BOLD, 24));
		
		container.add(label,BorderLayout.NORTH);
		container.add(add_bottons(),BorderLayout.EAST);
		container.add(add_table(), BorderLayout.CENTER);

		return container;
	}
	
	public JPanel add_bottons()
	{
		JPanel right = new JPanel();
		right.setLayout(new GridLayout(6,0));
	
		JButton New = new JButton("New StateObserver");
		New.addActionListener(new ActionListener()
		{
			@Override public void actionPerformed(ActionEvent arg0) 
			{
				display(new SOI_manageElement().show(self, false, null, updateStateObserverInv.availableTypes));
			}
		});
		JButton NewCxt = new JButton("Generate Basic StateObserver");
		NewCxt.addActionListener(new ActionListener()
		{
			@Override public void actionPerformed(ActionEvent arg0) 
			{
				updateStateObserverInv.generateBasicSO();
			}
		});
		JButton Modify = new JButton("Modify StateObserver");
		Modify.addActionListener(new ActionListener()
		{
			@Override public void actionPerformed(ActionEvent arg0) 
			{
				if(tab.getSelectedRow()==-1)	showMessage("Nothing Selected");
				else	display(new SOI_manageElement().show(self, true, updateStateObserverInv.main.OPEN_PRJ.stateObserverInv.list_stateObserver.get(tab.getSelectedRow()), updateStateObserverInv.availableTypes));
			}
		});
		JButton Delete = new JButton("Delete StateObserver");
		Delete.addActionListener(new ActionListener()
		{
			@Override public void actionPerformed(ActionEvent arg0) 
			{
				if(tab.getSelectedRow()==-1)	showMessage("Nothing Selected");
				else	updateStateObserverInv.deleteStateObserver(updateStateObserverInv.main.OPEN_PRJ.stateObserverInv.list_stateObserver.get(tab.getSelectedRow()));
			}
		});
		JButton Validate = new JButton("Validate StateObserver");
		Validate.addActionListener(new ActionListener()
		{
			@Override public void actionPerformed(ActionEvent arg0) 
			{
				if(tab.getSelectedRow()==-1)	showMessage("Nothing Selected");
				else	updateStateObserverInv.validate(updateStateObserverInv.main.OPEN_PRJ.stateObserverInv.list_stateObserver.get(tab.getSelectedRow()));
			}
		});
		JButton Close = new JButton("Close");
		
		Close.addActionListener(new ActionListener() 
		{
			@Override	public void actionPerformed(ActionEvent arg0) 
						{
							updateStateObserverInv.close();
						}
		});
		
		right.add(New);
		right.add(NewCxt);
		right.add(Modify);
		right.add(Delete);
		right.add(Validate);
		right.add(Close);
	
		return right;
	}
	
	@SuppressWarnings("serial")
	private JScrollPane add_table()
	{
		JPanel tabella = new JPanel();
		
		DefaultTableModel modelloRicerca = new DefaultTableModel();
		tab = new JTable(modelloRicerca){public boolean isCellEditable(int rowIndex, int vColIndex){return false;}@SuppressWarnings("unchecked")
			public java.lang.Class getColumnClass(int columnIndex) {return String.class;}};
		modelloRicerca.addColumn("State Observer ");
		modelloRicerca.addColumn("Invariant");		
		tab.setAutoCreateColumnsFromModel(false);								
		tab.getTableHeader().setReorderingAllowed(false);
		tab.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		tab.setFillsViewportHeight(true);
		int lines = 1;
		
		int max = 0;
		if(updateStateObserverInv.currentSOINV!=null && updateStateObserverInv.currentSOINV.getAll()!=null)
			max=updateStateObserverInv.currentSOINV.getAll().size();
		for (int i= 0; i<max; i++)
		{
			modelloRicerca.addRow(new Object[]{updateStateObserverInv.currentSOINV.get(i).getDescription(), updateStateObserverInv.currentSOINV.get(i).getExpr()});
			if(auxiliary_I_Team.getLineCount(updateStateObserverInv.currentSOINV.get(i).getDescription(),39)>lines || auxiliary_I_Team.getLineCount(updateStateObserverInv.currentSOINV.get(i).getExpr(),39)>lines)
				lines=Math.max(auxiliary_I_Team.getLineCount(updateStateObserverInv.currentSOINV.get(i).getExpr(),39),auxiliary_I_Team.getLineCount(updateStateObserverInv.currentSOINV.get(i).getDescription(),39));
		}
			  
		tab.setRowHeight(tab.getRowHeight() * lines);
		tab.setDefaultRenderer(String.class, new MultiLineCellRenderer());
		tabella.setLayout(new BorderLayout());
		tabella.add(tab.getTableHeader(), BorderLayout.NORTH);
		tabella.add(tab, BorderLayout.CENTER);
		
		JScrollPane scrollPane = new JScrollPane();
		scrollPane.getViewport().add(tabella);
		
		return scrollPane;
	}
	
	public void display(JPanel daVisualizzare)
	{
		container.removeAll();
		container.add(daVisualizzare);
		container.setVisible(true);
		container.revalidate();
		
	}
	
	public JPanel showSOI()
	{
		container.removeAll();
		container.add(init());
		container.setVisible(true);
		container.revalidate();
		
		return container;
	}

	public void showMessage(String string) 
	{
		JOptionPane.showMessageDialog(container, string, "Error", JOptionPane.WARNING_MESSAGE);
	}
	
}
